package org.tigr.htc.server.sge;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.util.StringTokenizer;

import org.tigr.antware.shared.util.Logger;
import org.tigr.antware.shared.util.RunSystemCommand;
import org.tigr.htc.server.IGridStatusUtils;

/**
 * The class <code>SgeGridStatusUtils</code> provides the methods to check the status of jobs
 * running under SGE.
 * 
 */

public class SgeGridStatusUtils implements IGridStatusUtils {
    /**
     * The variable <code>logger</code> holds the instance of the logger for this class.
     */
    private static Logger logger = new Logger(SgeGridStatusUtils.class);
    
    /**
     * The constructor <code>SgeGridStatusUtils</code> creates a new instance
     * 
     */
    public SgeGridStatusUtils() {
        super();
    }

    /* (non-Javadoc)
     * @see org.tigr.htc.server.IGridStatusUtils#getJobCount(java.lang.String, java.lang.String)
     */
    public int getJobCount(String user, String state) throws Throwable {
        if (logger.isDebugEnabled()) {
            logger.debug("Returning the job count for user " + user + " state " + state);
        }
        int count = 0;
        // Execute the qstat command to get the jobs for this user
        RunSystemCommand run = new RunSystemCommand();
        String command = "qstat";
        
        // If the user has specified the user to be null or all then get for
        // all users, otherwise get the status for just the specified user
        if ((user == null) || user.equalsIgnoreCase("all")) {
        	command += " -u *";
        } else {
            command += " -u " + user;
        }
       
        // Run the command and then grab the output and scan for output
        run.runCommand(command);
        String output = run.getOutString();
        
        if (logger.isDebugEnabled()) {
            logger.debug("Output: " + output);
        }
        StringTokenizer st = new StringTokenizer(output, "\n");
        
        // If the user has specified a state then parse the output to count the
        // strings for that state, otherwise jest return the total count
        if ((state != null) && (state.length() > 0)) {
            // Nothing
		} else {
            if (st.countTokens() > 0) {
                // Skip the first two tokens which are the row header and separator lines
                st.nextToken();
                st.nextToken();
                count = st.countTokens();
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Count " + count);
        }
        return count;
    }

}
